<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <link th:href="@{/css/update.css}" rel="stylesheet"/>
    <th:block th:include="include :: header('新增测试')" />
    <link th:href="@{/ajax/libs/summernote/summernote.css}" rel="stylesheet" type="text/css">

</head>
<body class="">
    <div class="main-content ">
        <form class="form-horizontal" id="form-test-add" th:object="${model}">
            <input name="id" th:field="*{id}" type="hidden">
            <input type="hidden" id="summernoteV" name="summernote" th:field="*{summernote}"/>
            <div class="row">
                <div id="summernote"></div>
            </div>
        </form>

    </div>
    <th:block th:include="include :: footer" />
    <script th:src="@{/ajax/libs/summernote/summernote.js}" type="text/javascript"></script>
    <script type="text/javascript" th:src="@{/ajax/libs/summernote/summernote-zh-CN.js}" ></script>
    <script type="text/javascript">
        var prefix = ctx + "netintech/test"
        $("#form-test-add").validate({
            focusCleanup: true
        });

        function submitHandler() {
            if ($.validate.form()) {
                $("#summernoteV").val($("#summernote").summernote('code'));
                $.operate.save(prefix + "/saveSummer", $('#form-test-add').serialize());
            }
        }

        $(document).ready(function () {
            $('#summernote').summernote({
                minHeight: 500,
                //tabsize: 2,
                lang: 'zh-CN',
                width: 980,
                //回调函数
                callbacks:{//回调函数，重写onImageUpload方法
                    onImageUpload: function(files) {
                        sendFile(this,files[0]);
                    },
                    onMediaDelete: function (target) {
                        var imgSrc = target.context.currentSrc;
                        var data = new FormData();
                        data.append("imgSrc", imgSrc);
                        console.log(imgSrc)
                        $.ajax({
                            data: data,
                            type: "POST",
                            url: "",
                            cache: false,
                            contentType: false,
                            processData: false,
                            success: function (data) {
                                console.log(data);
                            }
                        });
                    }
                }

            });
        });
        function sendFile(val, files){
            var formData = new FormData();
            formData.append("file", files);
            $.ajax({
                data: formData,
                dataType: 'json',
                type: "POST",
                url: ctx + "common/upload",
                cache: false,
                contentType: false,
                processData: false,
                responseType: "json",
                success: function(data) {
                    console.log("上传图片", data.url)
                    $(val).summernote('insertImage', data.url);
                },
                error: function(XMLHttpRequest, textStatus, errorThrown) {
                    alert(XMLHttpRequest.status);
                    alert(XMLHttpRequest.readyState);
                    alert(textStatus);
                }
            });
        }
        $(function(){
            var code = $("#summernoteV").val();
            $('#summernote').summernote('code', code);
        })

    </script>
</body>
</html>